package flex.cc.basicinformation.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import flex.cc.PublicUtil;
import flex.cc.basicinformation.service.PackageConfigExportService;
import flex.cc.basicinformation.service.StorePackageConfigService;
import flex.cc.store.dao.StoreActivityConfigDao;
import flex.cc.store.dto.entity.StoreActivityConfigEntity;
import flex.cc.store.dto.entity.StorePackageConfigEntity;
import flex.cc.system.dao.DictCodeDao;
import flex.cc.system.entity.DictCodeEntity;
import flex.cc.util.ExportUtil;

/**
 * @author R
 * 2022年02月22日 13:44:02
 * @desc 服务包配置导出
*/
@Service("packageConfigExportService")
public class PackageConfigExportServiceImpl implements PackageConfigExportService {

	@Resource
    private StorePackageConfigService storePackageConfigService;
	@Resource
    private StoreActivityConfigDao storeActivityConfigDao;
	@Resource
    private DictCodeDao dictCodeDao;
    
	/**
	 * @desc 服务包配置导出
	*/
	@Override
	public void downLoad(HttpServletRequest request, HttpServletResponse response, StorePackageConfigEntity storePackageConfigVo, String fileParentPath, String fileName) {
		// 表名、表头数据
		String title = fileName;
		String[] headers = {"服务包产品编码","服务包名称","类型","状态","服务项目编码","服务项目","服务次数","有效期","标识码","使用次数","服务商"};

		//根据条件获取服务包配置信息
		
		Map<String,Object> paramMap = new HashMap<String,Object>();
		String configCode = storePackageConfigVo.getConfigCode();
		String configName = storePackageConfigVo.getConfigName();
		String startTime = storePackageConfigVo.getStartTime();
		String endTime = storePackageConfigVo.getEndTime();
		
		if(PublicUtil.isNotEmpty(configCode)){
			paramMap.put("configCode", configCode);
		}
		if(PublicUtil.isNotEmpty(configName)){
			paramMap.put("configName", configName);
		}
		if(PublicUtil.isNotEmpty(startTime)){
			paramMap.put("startTime", startTime);
		}
		if(PublicUtil.isNotEmpty(endTime)){
			paramMap.put("endTime", endTime);
		}
		List<StorePackageConfigEntity> configRecords = storePackageConfigService.queryPackageConfig(paramMap);
		
		Map<String,List<StoreActivityConfigEntity>> activityConfigEntityMap = new HashMap<String,List<StoreActivityConfigEntity>>();
		//遍历服务包配置信息 获取服务项目信息
		for(int i=0; i<configRecords.size(); i++){
			StorePackageConfigEntity storePackageConfigEntity = configRecords.get(i);
			//获取服务包类型
			String packageType = storePackageConfigEntity.getPackageType();
			//根据服务包类型查询字典表转义中文
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("typeCode", "packageType");
			params.put("code", packageType);
			DictCodeEntity dictCodeEntity = dictCodeDao.getOneByParams(params);
			storePackageConfigEntity.setPackageType(dictCodeEntity.getValue());
			//序列码类型
			storePackageConfigEntity.setStatus("1".equals(storePackageConfigEntity.getStatus())? "禁用" : "启用" );	
			
			String configCodeStr = storePackageConfigEntity.getConfigCode();
			Map<String, Object> reqVo = new HashMap<String, Object>();
			reqVo.put("configCode", configCodeStr);
			List<StoreActivityConfigEntity> activityConfigEntityList = storeActivityConfigDao.queryActivityConfig(reqVo);
			activityConfigEntityMap.put(configCodeStr, activityConfigEntityList);
		}
		if (!PublicUtil.isEmpty(configRecords)) {
			try {
				SXSSFWorkbook wb = ExportUtil.createWorkbook(10, false);//创建工作簿对象
				ExportUtil exportUtil = new ExportUtil();
				exportUtil.downLoadPackageConfig(title, headers, wb, configRecords,activityConfigEntityMap);
				File fileParent = new File(fileParentPath);
				if (!fileParent.exists()) { // 若目录不存在则创建
					fileParent.mkdirs();
				}

				File file = new File(fileParentPath + File.separator + fileName + ".xlsx");
				if (file.exists()) { // 若文件已存在则删除
					file.delete();
				}
				try {
					wb.write(new FileOutputStream(file));
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
